package com.jinzhuo.music.controller;

import com.alibaba.fastjson.JSONObject;
import com.jinzhuo.music.domain.Singer;
import com.jinzhuo.music.domain.Song;
import com.jinzhuo.music.service.SongService;
import com.jinzhuo.music.utils.Consts;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * 歌曲管理controller
 */
@RestController
@RequestMapping("/song")
public class SongController {
    @Autowired
    private SongService songService;
    /**
     * 添加歌曲
     */
    @RequestMapping(value = "addSong",method = RequestMethod.POST)
    public Object addSong(HttpServletRequest request, @RequestParam("file")MultipartFile multipartFile){
        JSONObject jsonObject = new JSONObject();
        String singerId = request.getParameter("singerId").trim();
        String name = request.getParameter("name").trim();
        String introduction = request.getParameter("introduction").trim();
        String pic = "/img/songPic/song.jpeg";
        String lyric = request.getParameter("lyric").trim();
        //上传歌曲文件
        if (multipartFile.isEmpty()){
            jsonObject.put(Consts.CODE,0);
            jsonObject.put(Consts.MSG,"歌曲上传失败");
            return jsonObject;
        }
        //文件名=当前时间的毫秒+原来的文件名
        String fileName=System.currentTimeMillis()+multipartFile.getOriginalFilename();
        //文件路径
        String filePath=System.getProperty("user.dir")+System.getProperty("file.separator")+"song";
        //如果文件路径不存在，新增路径
        File file = new File(filePath);
        if (!file.exists()){
            file.mkdir();
        }
        //实际的文件地址
        File dest = new File(filePath + System.getProperty("file.separator") + fileName);
        //存储到数据库里的相对文件地址
        String storeUrlPath = "/song/"+fileName;
        try {
            multipartFile.transferTo(dest);
            Song song = new Song();
            song.setSingerId(Integer.parseInt(singerId));
            song.setPic(pic);
            song.setIntroduction(introduction);
            song.setUrl(storeUrlPath);
            song.setName(name);
            song.setLyric(lyric);
            boolean flag = songService.addSong(song);
            if (flag){
                jsonObject.put(Consts.CODE,1);
                jsonObject.put(Consts.MSG,"保存成功");
                jsonObject.put("avator",storeUrlPath);
                return jsonObject;
            }else {
                jsonObject.put(Consts.CODE,0);
                jsonObject.put(Consts.MSG,"保存失败");
                return jsonObject;
            }
        } catch (IOException e) {
            jsonObject.put(Consts.CODE,0);
            jsonObject.put(Consts.MSG,"保存失败"+e.getMessage());
        }finally {
            return jsonObject;
        }
    }
    /**
     * 根歌手id查询歌曲
     */
    @RequestMapping(value = "querySongBySingerId",method = RequestMethod.POST)
    public Object querySongBySingerId(HttpServletRequest request){
        String singerId = request.getParameter("singerId");
        return  songService.querySongBySingerId(Integer.parseInt(singerId));
    }

    /**
     * 根歌曲id查询歌曲对象
     */
    @RequestMapping(value = "querySongBySongId",method = RequestMethod.POST)
    public Object querySongBySongId(HttpServletRequest request){
        String songId = request.getParameter("songId");
        return  songService.getSong(Integer.parseInt(songId));
    }

    /**
     * 根歌曲名称查询歌曲对象
     */
    @RequestMapping(value = "querySongBySongName",method = RequestMethod.POST)
    public Object querySongBySongName(HttpServletRequest request){
        String songName = request.getParameter("songName");
        return  songService.querySongByName(songName);
    }

    /**
     * 根据歌名模糊查询歌曲
     */
    @RequestMapping(value = "querySongLikeName",method = RequestMethod.POST)
    public Object querySongLikeName(HttpServletRequest request){
        String songName = request.getParameter("songName");
        return  songService.querySongLikeName(songName);
    }

    /**
     * 查询所有歌曲
     */
    @RequestMapping(value = "queryAllSong",method = RequestMethod.POST)
    public Object queryAllSong(HttpServletRequest request){
        return  songService.queryAllSong();
    }

    /**
     * 修改歌曲
     */
    @RequestMapping(value = "updateSong",method = RequestMethod.POST)
    public Object updateSong(HttpServletRequest request){
        JSONObject object = new JSONObject();
        String id = request.getParameter("id").trim();
        String name = request.getParameter("name").trim();
        String introduction = request.getParameter("introduction").trim();
        String lyric = request.getParameter("lyric").trim();
        //保存到歌曲的对象中
        Song song = new Song();
        song.setId(Integer.parseInt(id));
        song.setName(name);
        song.setIntroduction(introduction);
        song.setLyric(lyric);
        boolean flag = songService.updateSong(song);
        if (flag){
            object.put(Consts.CODE,"1");
            object.put(Consts.MSG,"修改成功");
        }else {
            object.put(Consts.CODE,0);
            object.put(Consts.MSG,"修改失败");
        }
        return object;
    }

    /**
     * 删除
     */
    @RequestMapping(value = "deleteSong",method = RequestMethod.POST)
    public Object deleteSong(HttpServletRequest request) {
        JSONObject object = new JSONObject();
        //TODO 先查询到数据库中的歌曲文件然后删除，再执行后续的代码
        String id = request.getParameter("id").trim();
        boolean flag = songService.deleteSong(Integer.parseInt(id));
        return flag;
    }

    /**
     * 更新歌曲图片
     *
     */
    @RequestMapping(value = "updateSongPic",method = RequestMethod.POST)
    public Object updateSongPic(@RequestParam("file")MultipartFile avatorFile,@RequestParam("id")int id){
        JSONObject jsonObject = new JSONObject();
        if (avatorFile.isEmpty()){
            jsonObject.put(Consts.CODE,0);
            jsonObject.put(Consts.MSG,"图像上传失败");
            return jsonObject;
        }
        //文件名=当前时间的毫秒+原来的文件名
        String fileName=System.currentTimeMillis()+avatorFile.getOriginalFilename();
        //文件路径
        String filePath=System.getProperty("user.dir")+System.getProperty("file.separator")+"img"
                +System.getProperty("file.separator")+"songPic";
        //如果文件路径不存在，新增路径
        File file = new File(filePath);
        if (!file.exists()){
            file.mkdir();
        }
        //实际的文件地址
        File dest = new File(filePath + System.getProperty("file.separator") + fileName);
        //存储到数据库里的相对文件地址
        String storeAvatorPath = "/img/songPic/"+fileName;
        try {
            avatorFile.transferTo(dest);
            Song song = new Song();
            song.setId(id);
            song.setPic(storeAvatorPath);
            boolean flag = songService.updateSong(song);
            if (flag){
                jsonObject.put(Consts.CODE,1);
                jsonObject.put(Consts.MSG,"歌曲图片上传成功");
                return jsonObject;
            }else {
                jsonObject.put(Consts.CODE,0);
                jsonObject.put(Consts.MSG,"歌曲图片上传失败");
                return jsonObject;
            }
        } catch (IOException e) {
            jsonObject.put(Consts.CODE,0);
            jsonObject.put(Consts.MSG,"上传失败"+e.getMessage());
        }finally {
            return jsonObject;
        }
    }

    /**
     * 更新歌曲文件地址
     *
     */
    @RequestMapping(value = "updateSongUrl",method = RequestMethod.POST)
    public Object updateSongUrl(@RequestParam("file")MultipartFile avatorFile,@RequestParam("id")int id){
        JSONObject jsonObject = new JSONObject();
        if (avatorFile.isEmpty()){
            jsonObject.put(Consts.CODE,0);
            jsonObject.put(Consts.MSG,"歌曲上传失败");
            return jsonObject;
        }
        //文件名=当前时间的毫秒+原来的文件名
        String fileName=System.currentTimeMillis()+avatorFile.getOriginalFilename();
        //文件路径
        String filePath=System.getProperty("user.dir")+"/song";
        //如果文件路径不存在，新增路径
        File file = new File(filePath);
        if (!file.exists()){
            file.mkdir();
        }
        //实际的文件地址
        File dest = new File(filePath + System.getProperty("file.separator") + fileName);
        //存储到数据库里的相对文件地址
        String storeAvatorPath = "/song/"+fileName;
        try {
            avatorFile.transferTo(dest);
            Song song = new Song();
            song.setId(id);
            song.setUrl(storeAvatorPath);
            boolean flag = songService.updateSong(song);
            if (flag){
                jsonObject.put(Consts.CODE,1);
                jsonObject.put(Consts.MSG,"歌曲图片上传成功");
                jsonObject.put("avatr",storeAvatorPath);
                return jsonObject;
            }else {
                jsonObject.put(Consts.CODE,0);
                jsonObject.put(Consts.MSG,"歌曲图片上传失败");
                return jsonObject;
            }
        } catch (IOException e) {
            jsonObject.put(Consts.CODE,0);
            jsonObject.put(Consts.MSG,"上传失败"+e.getMessage());
        }finally {
            return jsonObject;
        }
    }

}
